var createError = require('http-errors');
var express = require('express');
var ueditor = require("ueditor");
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
var bodyParser = require('body-parser')
var ueditor = require("ueditor")
var ejs = require('ejs');

var indexRouter = require('./routes/index');

var pushupRouter = require('./routes/pushup');
var doubanRouter = require('./routes/douban');
var watchRouter = require('./routes/watch');
var articleRouter = require('./routes/article');
var chartRouter = require('./routes/char');
var manarticleRouter = require('./routes/manarticle');
var registerRouter = require('./routes/register');
var projectRouter = require('./routes/project');
var exprienceRouter = require('./routes/exprience');
var aboutRouter = require('./routes/about');
var messageRouter = require('./routes/message');
var manprojectRouter = require('./routes/manproject');
var appRouter = require('./routes/app');

var apiPushupRouter = require('./api/pushup');
var apiarticleRouter = require('./api/article');
var apiUsersRouter = require('./api/users');
var apiloginRouter = require('./api/login');
var apiprojectRouter = require('./api/project');
var apiexprienceRouter = require('./api/exprience');
var apicommentsRouter = require('./api/comments');
var apimessageRouter = require('./api/message');
var apiaboutRouter = require('./api/about');





var app = express();
app.use(bodyParser.urlencoded({
    extended: true
}));
app.use(bodyParser.json());
app.use(express.static(path.join(__dirname, 'public')));
app.set('views', path.join(__dirname, 'views'));
app.engine('.html', ejs.__express);
app.set('view engine', 'html');
app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser('lifeissimpebutyoumadeitcomplicated'));

// app.use(express.static(path.join(__dirname, 'public')));
// app.use(express.static(path.join(__dirname, 'uploads')));

app.use("/ueditor/ue", ueditor(path.join(__dirname, 'public'), function (req, res, next) {
  //客户端上传文件设置
  var imgDir = '/img/ueditor/'
   var ActionType = req.query.action;
  if (ActionType === 'uploadimage' || ActionType === 'uploadfile' || ActionType === 'uploadvideo') {
      var file_url = imgDir;//默认图片上传地址
      /*其他上传格式的地址*/
      if (ActionType === 'uploadfile') {
          file_url = '/file/ueditor/'; //附件
      }
      if (ActionType === 'uploadvideo') {
          file_url = '/video/ueditor/'; //视频
      }
      res.ue_up(file_url); //你只要输入要保存的地址 。保存操作交给ueditor来做
      res.setHeader('Content-Type', 'text/html');
  }
  //  客户端发起图片列表请求
  else if (req.query.action === 'listimage') {
      var dir_url = imgDir;
      res.ue_list(dir_url); // 客户端会列出 dir_url 目录下的所有图片
  }
  // 客户端发起其它请求
  else {
      // console.log('config.json')
      res.setHeader('Content-Type', 'application/json');
      res.redirect('/ueditor/nodejs/config.json');
  }
}));

app.all('/api/*', function(req, res, next) {
    res.header("Access-Control-Allow-Origin", "*");
    res.header('Access-Control-Allow-Methods', 'PUT, GET, POST, DELETE, OPTIONS');
    res.header("Access-Control-Allow-Headers", "X-Requested-With");
    res.header('Access-Control-Allow-Headers', 'Content-Type');
    next();
});
app.all('*', function(req, res, next) {
  res.header("Access-Control-Allow-Origin", "*");
  res.header('Access-Control-Allow-Methods', 'PUT, GET, POST, DELETE, OPTIONS');
  res.header("Access-Control-Allow-Headers", "X-Requested-With");
  res.header('Access-Control-Allow-Headers', 'Content-Type');
  next();
});
app.all('/pushup/*', function (req, res, next) {
  if (req.cookies.isLogin === 'ok') {
    next()
  } else {
    res.redirect('/login')
  }
})
app.all('/article', function (req, res, next) {
  if (req.cookies.isLogin === 'ok') {
    next()
  } else {
    res.redirect('/login')
  }
})

app.use('/', indexRouter);

app.use('/pushup', pushupRouter);
app.use('/chart', chartRouter);
app.use('/douban', doubanRouter);
app.use('/watch', watchRouter);
app.use('/project', projectRouter);
app.use('/article', articleRouter);
app.use('/manarticle', manarticleRouter);
app.use('/register', registerRouter);
app.use('/exprience', exprienceRouter);
app.use('/about', aboutRouter);
app.use('/message', messageRouter);
app.use('/manproject', manprojectRouter);
app.use('/app', appRouter);

app.use('/api/pushup', apiPushupRouter);
app.use('/api/users', apiUsersRouter);
app.use('/api/article', apiarticleRouter);
app.use('/api/login', apiloginRouter);
app.use('/api/comments', apicommentsRouter);
app.use('/api/project', apiprojectRouter);
app.use('/api/exprience', apiexprienceRouter);
app.use('/api/message', apimessageRouter);
app.use('/api/about', apiaboutRouter);

// catch 404 and forward to error handler
// app.use(function(req, res, next) {
//   next(createError(404));
// });

// error handler
// app.use(function(err, req, res, next) {
//   // set locals, only providing error in development
//   res.locals.message = err.message;
//   res.locals.error = req.app.get('env') === 'development' ? err : {};

//   // render the error page
//   res.status(err.status || 500);
//   res.render('error');
// });

module.exports = app;
